const { hospitalModel, menuAndJobModel, doctorAndJobModel } = require("../model");
const { createToken } = require("../utils/jwt");
//登录
exports.loginHospital = async (req, res) => {
  const { phone, password } = req.body;
  console.log(phone,password);
  try {
    const result = await hospitalModel
      .findOne({ phone, password })
      .populate("depart", "DepartName DepartContent");
      if(!result){
       return res.status(404).json({
        code: 404,
        message: "账号或者密码不正确",
        data: null,
      }); 
      }
      if(result.isAutonym==2) {
        return res.status(400).json({
          code:400,
          message:'该账号以离职,无法登录',
          data:null
        })
      }
    let job = result._doc.job;

    let menus = await (
      await menuAndJobModel.find({ jid: job }).populate("mid", "mName mValue")
    ).map((item) => {
      return {
        mName: item.mid.mName,
        mValue: item.mid.mValue,
      };
    });
    delete result._doc.job;
    result._doc.menus = menus;
   
      let token = await createToken(result._doc);
      result._doc.token = token;
      res.status(200).json({
        code: 200,
        message: "登录成功",
        data: result,
      });
   
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: "服务器错误",
      data: null,
    });
  }
};
//添加医生
exports.addDoctor = async (req, res) => {
  const { name, sex, age, photo, phone, password, depart, TweDepart, job } =
    req.body;
  //判断信息是否完整
  if (
    !name ||
    !sex ||
    !age ||
    !photo ||
    !phone ||
    !depart ||
    !TweDepart ||
    !job
  ) {
    res.status(400).json({
      code: 400,
      message: "信息不完整",
      data: null,
    });
  }
  try {
    if (await hospitalModel.findOne({ phone })) {
      return res.status(400).json({
        code: 400,
        message: "该手机号已注册",
        data: null,
      });
    }
    let dbBack = new hospitalModel({
      name,
      sex,
      age,
      photo,
      phone,
      password,
      depart,
      TweDepart,
      job,
    });
   
    await dbBack.save();
    let docs = await hospitalModel.findOne({ phone });
    let  dbback2 = new doctorAndJobModel({
      jid:job,
      hid:docs._id
    })
    await dbback2.save();
    res.status(200).json({
      code: 200,
      message: "添加成功",
      data: dbBack,
    });
   
   
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: "服务器错误,请稍后重试",
      data: error,
    });
  }
};
//查询医生列表
exports.getDoctorList = async (req, res) => {
  const { pageNo = 1, pageSize = 10 } = req.query;
  try {
    let result = await hospitalModel
      .find()
      .skip((pageNo - 1) * pageSize)
      .limit(pageSize)
      .populate("depart", "DepartName DepartContent")
      .populate("job");
    let total = await hospitalModel.find().count();
    res.status(200).json({
      code: 200,
      message: "查询成功",
      data: result,
      total,
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: "查询失败",
      data: error,
    });
  }
};
//查看医生详情
exports.getDoctorDetail = async (req, res) => {
  let id = req.query.id;
  if (!id) {
    return res.status(400).json({
      code: 400,
      message: "id不能为空",
      data: null,
    });
  }
  try {
    let result = await hospitalModel
      .findById(id)
      .populate("depart", "DepartName DepartContent")
      .populate("job");
    res.status(200).json({
      code: 200,
      message: "查询成功",
      data: result,
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: "查询失败",
      data: error,
    });
  }
};
//修改医生信息
exports.updateDoctor = async (req, res) => {
  let id = req.query.id;
  if (!id) {
    return res.status(400).json({
      code: 400,
      message: "id不能为空",
      data: null,
    });
  }
  const { name, sex, age, photo, phone, password, depart, TweDepart, job , isAutonym } =req.body;
  try {
   await hospitalModel.findByIdAndUpdate({_id:id},{name,sex,age,photo,phone,password,depart,TweDepart,job,isAutonym});
   await doctorAndJobModel.updateOne({hid:id},{jid:job})
   res.status(200).json({
       code: 200,
       message: "修改成功",
       data: null,
   })
  } catch (error) {
    res.status(500).json({
        code: 500,
        message: "服务器错误,请稍后重试",
        data: error,
    })
  }
};

